home *** CD-ROM | disk | FTP | other *** search
/ AMIGA-CD 2 / Amiga-CD - Volume 2.iso / ungepackte_daten / 1993 / 4 / 02 / semaphoren / second.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-01  |  1.2 KB  |  46 lines

  1. /* Dieses Programm wird von »Init« aufgerufen
  2.  * (nur ab OS 2.0).
  3.  * Autor: Rainer Zeitler
  4.  */
  5. #include <exec/types.h>
  6. #include <exec/ports.h>
  7. #include <exec/semaphores.h>
  8.  
  9. char *SemName="mysema"; /* Name der Semaphore */
  10.  
  11. struct MyMessage {
  12.   struct Message msg;
  13.   struct MsgPort *ReplyPort;
  14.   ULONG *change; /* Diese Variable ändern */
  15. } MsgSend, *MsgReceived;
  16.  
  17. struct MsgPort *TestPort, *ReplyPort;
  18. struct SignalSemaphore *mysema;
  19.  
  20. main() {
  21.   ULONG count=0,*change;
  22.   if( (TestPort=(struct MsgPort *)
  23.             FindPort("Test-Semaphore")) != NULL ) {
  24.     mysema=FindSemaphore(SemName);
  25.     ReplyPort= CreatePort(0,0);
  26.     MsgSend.msg.mn_Length=sizeof(struct MyMessage);
  27.     MsgSend.msg.mn_Node.ln_Type=NT_MESSAGE;
  28.     MsgSend.msg.mn_ReplyPort=NULL;
  29.     MsgSend.ReplyPort=ReplyPort;
  30.     /* Init mitteilen, daß wir bereit sind */
  31.     PutMsg(TestPort,&MsgSend);
  32.     /* Auf die Message von Init warten */
  33.     WaitPort( ReplyPort );
  34.     MsgReceived=GetMsg( ReplyPort );
  35.     /* Diesen Speicherbereich modifizieren */
  36.     change=MsgReceived->change;
  37.     /* Semaphore reservieren */
  38.     ObtainSemaphore(mysema);
  39.     PutMsg(TestPort,&MsgSend);
  40.     while( count < 1000000 )
  41.       { *change+=1; count++; }
  42.     /* Und wieder freigeben */
  43.     ReleaseSemaphore(mysema);
  44.   }
  45. }
  46.